package com.qxkj.demo.contoller;

import com.qxkj.demo.pojo.CeShi;
import com.qxkj.demo.service.CeShiService;
import com.rabbitmq.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeoutException;

@RestController
public class CsController {
    private final static String QUEUE_NAME = "e_test";
    @Resource
    private CeShiService ceShiService;
    @RequestMapping("/select")
    public List getAll(){
        List<CeShi> list = ceShiService.getAll();
        return list;
    }
    @RequestMapping("/update")
    public int update(CeShi ceShi){
        int aa = ceShiService.update(ceShi);
        return aa;
    }
    @RequestMapping("/delete")
    public int delete(Integer id){
        int aa = ceShiService.delete(id);
        return aa;
    }

    /**
     * RabbitMQ消息发送者
     * @param message
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    @GetMapping("/send")
    @ResponseBody
    public String send(String message) throws IOException, TimeoutException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        // 设置 RabbitMQ 的主机名
        factory.setHost("115.159.40.126");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("Alex1595..");
        // 创建一个连接
        Connection connection = factory.newConnection();
        // 创建一个通道
        Channel channel = connection.createChannel();
        // 指定一个队列
        // queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        // 参数1 queue ：队列名
        // 参数2 durable ：是否持久化
        // 参数3 exclusive ：仅创建者可以使用的私有队列，断开后自动删除
        // 参数4 autoDelete : 当所有消费客户端连接断开后，是否自动删除队列
        // 参数5 arguments
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 发送消息
        /*String message = "Hello World!";*/
        // basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
        // 参数1 exchange ：交换器
        // 参数2 routingKey ： 路由键
        // 参数3 props ： 消息的其他参数
        // 参数4 body ： 消息体
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        // 关闭频道和连接
        channel.close();
        connection.close();
        return message;
    }
    String message = "";

    /**
     * 消息接收者
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    @GetMapping("/Recv")
    @ResponseBody
    public String Recv() throws IOException, TimeoutException{
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
// 设置MabbitMQ, 主机ip或者主机名
        factory.setHost("115.159.40.126");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("Alex1595..");
// 创建一个连接
        Connection connection = factory.newConnection();
// 创建一个通道
        Channel channel = connection.createChannel();
// 指定一个队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] 等待消息进入. 请按 CTRL+C 结束");
// 创建队列消费者
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                message = new String(body, "UTF-8");
                System.out.println(" [x] 接收消息是： '" + message + "'");
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
        return message;
    }
}
